import pyautogui
import time
import 手势动作模块_动作判断_单手模式_开掌版 as motion

motion = motion.MotionDetection()


class HandAction():
    def __init__(self):
        self.flag = "None"
    def action(self, type, lmList):
        # 开掌
        if motion.openPalm(lmList):
            if self.flag == "openPalm":
                return
            self.flag = "openPalm"
            self.releaseAllKey()

        else:
            self.right(lmList)
            
    # 操作
    def right(self, lmList):
        if motion.littleFinger(lmList):
            # print("左方向键（左转)(小拇指)")
            if self.flag == "rightLittleFinger":
                return
            self.flag = "rightLittleFinger"
            pyautogui.keyDown('left')

        elif motion.indexFinger(lmList):
            if self.flag == "rightIndexFinger":
                return
            self.flag = "rightIndexFinger"
            # print("右方向键(右转)(小拇指)")
            pyautogui.keyDown('right')
        elif motion.middleFingerAndIndexFinger(lmList):
            if self.flag == "middleFingerAndIndexFinger":
                return
            self.flag = "middleFingerAndIndexFinger"
            pyautogui.keyDown('right')
            pyautogui.keyDown("shift")

        elif motion.pinkyAndRingFinger(lmList):
            if self.flag == "pinkyAndRingFinger":
                return
            self.flag = "pinkyAndRingFinger"
            # print("左漂移(无名指+小拇指)")
            pyautogui.keyDown('left')
            pyautogui.keyDown("shift")
        elif motion.ringFinger(lmList):
            if self.flag == "ringFinger":
                return
            self.flag = "ringFinger"
            # print("上方向键(小喷)(无名指)")
            pyautogui.press('up')
        elif motion.middleFinger(lmList):
            if self.flag == "rightThumbsUp":
                return
            self.flag = "rightThumbsUp"
            # print("ctrl(氮气)(握拳)")
            pyautogui.press('ctrl')
        # elif motion.makeAFist(lmList):
        #     if self.flag == "makeAFist":
        #         return
        #     self.flag = "makeAFist"
        #     # self.releaseAllKey()
        #     pyautogui.press('ctrl')
        elif motion.loseIndexFinger(lmList):
            if self.flag == "loseRightIndexFinger":
                return
            self.flag = "loseRightIndexFinger"
            pyautogui.keyUp('right')
        elif motion.loseLittleFinger(lmList):
            if self.flag == "loseRightLittleFinger":
                return
            self.flag = "loseRightLittleFinger"
            # print("释放2")
            pyautogui.keyUp('left')
        elif motion.loseMiddleFingerAndIndexFinger(lmList):
            if self.flag == "loseMiddleFingerAndIndexFinger":
                return
            self.flag = "loseMiddleFingerAndIndexFinger"
            # print("释放3")
            pyautogui.keyUp('right')
            pyautogui.keyUp("shift")
        elif motion.losePinkyAndRingFinger(lmList):
            if self.flag == "losePinkyAndRingFinger":
                return
            self.flag = "losePinkyAndRingFinger"
            # print("释放4")
            pyautogui.keyUp('left')
            pyautogui.keyUp("shift")

    # # 左手操作
    # def left(self, lmList):
    #     if motion.indexFinger(lmList) and self.flag != "leftIndexFinger":
    #         self.flag = "leftIndexFinger"
    #         print("漂移(shift)(左手食指)")
    #         pyautogui.keyDown('shift')
    #     elif motion.loseIndexFinger(lmList) and self.flag != "loseIndexFinger":
    #         self.flag = "loseIndexFinger"
    #         pyautogui.keyUp('shift')
    #         pyautogui.press("left")
    #         pyautogui.press("right")
    #
    #     elif motion.littleFinger(lmList) and self.flag != "leftIndexFinger":
    #         self.flag = "leftIndexFinger"
    #         print("氮气加速(ctrl)(小拇指)")
    #         pyautogui.press('ctrl')

    def releaseAllKey(self):
        pyautogui.keyUp('shift')
        pyautogui.keyUp('left')
        pyautogui.keyUp('right')

    # # 共同操作
    # def coOperation(self, lmList):
    #     if self.littleFinger(lmList):
    #         print("氮气加速(ctrl)(小拇指)")
    #         pyautogui.press('ctrl')
    #     elif self.middleFinger(lmList):
    #         print("上方向键(小喷)(中指)")
    #         pyautogui.press('up')
    #     elif self.ringFinger(lmList):
    #         print("R(复原)(无名指)")
    #         pyautogui.press('R')
    #     else:
    #         print("不做事情")

    # 动作判断
    # 因为使用opencv的方向，高度是从左上角算的。左上角是0,0,这与现实世界是不同的
    # 握拳
    def makeAFist(self, lmList):
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] < lmList[10][1] and lmList[16][1] < lmList[14][1] and
                lmList[20][1] < lmList[18][1] and lmList[4][1] < lmList[2][1]):
            return True
        return False

    # 食指
    def indexFinger(self, lmList):
        # 1指向右转
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] > lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            return True
        return False

    # 中指和食指
    def middleFingerAndIndexFinger(self, lmList):
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] < lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            return True
        return False

    # 中指
    def middleFinger(self, lmList):
        if (lmList[8][1] > lmList[6][1] and lmList[12][1] < lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            return True
        return False

    # 小拇指
    def littleFinger(self, lmList):
        if (lmList[8][1] > lmList[6][1] and lmList[12][1] > lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] < lmList[18][1]):
            return True
        return False

    # 无名指
    def ringFinger(self, lmList):
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] < lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] < lmList[18][1]):
            return True
        return False

    # 开掌
    def openPalm(self, lmList):
        if (lmList[8][1] > lmList[6][1] and lmList[12][1] > lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            return True
        return False
